﻿#region Copyright and License Notice
// Copyright (C)2010-2016 - Rob Levine and other contributors
// http://configgen.codeplex.com
// 
// This file is part of ConfigGen.
// 
// ConfigGen is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// ConfigGen is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License and 
// the GNU Lesser General Public License along with ConfigGen.  
// If not, see <http://www.gnu.org/licenses/>
#endregion

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConfigGen.Utilities.Extensions.System
{
    /// <summary>
    /// Extensions for arrays.
    /// </summary>
    public static class ArrayExtensions
    {
        /// <summary>
        /// Returns true if the current array is null or zero length, otherwise false.
        /// </summary>
        /// <returns>true if the current array is null or zero length, otherwise false.</returns>
        public static bool IsNullOrEmpty(this Array array)
        {
            return array == null || array.Length == 0;
        }

        /// <summary>
        /// Returns true if the current instance is value-equal to the the supplied array, otherwise false.
        /// </summary>
        /// <remarks>Two arrays are considered equal if (and only if) they are the same length, and each individual element is value-equal 
        /// to the corresponding element in the other array, or if both arrays are null.</remarks>
        /// <param name="array1">First array</param>
        /// <param name="array2">Second array</param>
        /// <returns>True if equal, otherwise false.</returns>
        public static bool IsEqualTo<T>(this T[] array1, T[] array2)
        {
            if ((array1 == null) ^ (array2 == null)) return false;
            if (array1 == null) return true;

            if (ReferenceEquals(array1, array2))
            {
                return true;
            }

            if (array1.Length != array2.Length)
            {
                return false;
            }

            for (var i = 0; i < array1.Length; i++)
            {
                if (!array1[i].Equals(array2[i]))
                {
                    return false;
                }
            }

            return true;
        }

        /// <summary>
        /// Returns true if the supplied collection is null or empty, or it contains only null, DBNull or empty string values, otherwise false.
        /// </summary>
        /// <param name="dataCollection">Collection</param>
        /// <returns>true if the supplied collection is null or empty, or it contains only null, DBNull or empty string values, otherwise false</returns>
        public static bool IsCollectionOfNullOrEmpty(this IEnumerable<object> dataCollection)
        {
            if (dataCollection == null || ! dataCollection.Any()) return true;
            return dataCollection.All(item => (item == null || item is DBNull || (item is string && string.IsNullOrEmpty((string)item))));
        }
    }
}
